package com.custom.action.condition

import com.custom.comm.exceptions.CustomCheckException
import com.custom.comm.utils.JudgeUtil
import kotlin.reflect.KProperty1

/**
 * @author XiaoBai
 * @since 2024/1/4 13:36
 *
 */
class KtConditionWrapper<T>(target: Class<T>) : AbstractConditionWrapper<T, KProperty1<T, *>, KtConditionWrapper<T>>() {

    init {
        wrapperInitialize(target)
    }

    constructor(wrapper: ConditionWrapper<T>) :this(wrapper.entityClass) {
            this.dataStructureInit()
            setEntityClass(wrapper.entityClass)
            columnParseHandler = wrapper.columnParseHandler
            tableSupport = wrapper.tableSupport
            lastCondition = wrapper.lastCondition
            addCondition(wrapper.finalConditional)
            this.addParams(wrapper.paramValues)
            selectColumns = wrapper.selectColumns
            setPageParams(wrapper.getPageIndex(), wrapper.getPageSize())
            primaryTable = wrapper.primaryTable
    }

    override fun paramTransColumn(column: KProperty1<T, *>?): String {
        if (column == null) {
            throw NullPointerException()
        }
        val sqlColumn = tableSupport.fieldMap()[column.name]
        if (JudgeUtil.isEmpty(column)) {
            throw CustomCheckException("未知的column：" + column.name)
        }
        return sqlColumn!!
    }

    override fun getInstance(): KtConditionWrapper<T> {
        return KtConditionWrapper(entityClass)
    }


    fun toDefault() :DefaultConditionWrapper<T> {
        return DefaultConditionWrapper(entityClass)
    }

}